package com.rocksea.app.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.rocksea.app.R;
import com.rocksea.app.http.HttpUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;

public class UploadLogsActivity extends AppCompatActivity
{

    public final static String LOG_KEY = "LOG_KEY";
    private ArrayList<String> logs;
    private LogAdapter mLogAdapter;
    private ListView mListLog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_upload_log_layout);

        ActionBar mActionBar = getSupportActionBar();
        if (mActionBar!=null)
        {
            mActionBar.setTitle("上传日志");
            mActionBar.setHomeButtonEnabled(true);
            mActionBar.setDisplayHomeAsUpEnabled(true);
        }
        String[] log = getIntent().getStringArrayExtra(LOG_KEY);
        logs = new ArrayList<>();
        Collections.addAll(logs,log);
        mListLog = (ListView) findViewById(R.id.logListView);
        mLogAdapter = new LogAdapter();
        mListLog.setAdapter(mLogAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.clear,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                finish();
                break;
            case R.id.clear:
                logs.clear();
                mLogAdapter.notifyDataSetChanged();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy()
    {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    private class LogAdapter extends BaseAdapter
    {

        @Override
        public int getCount()
        {
            return logs.size();
        }

        @Override
        public Object getItem(int position)
        {
            return null;
        }

        @Override
        public long getItemId(int position)
        {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            if (convertView == null)
                convertView = View.inflate(getBaseContext(),android.R.layout.simple_list_item_activated_1,null);
            TextView log = (TextView) convertView.findViewById(android.R.id.text1);
            log.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
            log.setText(logs.get(position));
            return convertView;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUploadEvent(HttpUtil.UploadEvent event)
    {
        logs.add(String.format("%s %s",event.time,event.message));
        mLogAdapter.notifyDataSetChanged();
        if (mLogAdapter.getCount() == 0)
            return;
        mListLog.setSelection(mLogAdapter.getCount() - 1);
    }

}

